package cn.ht.jywrwater.module.ht.service.waterqualityalertcount;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.ht.jywrwater.module.ht.controller.admin.waterqualityalertcount.vo.*;
import cn.ht.jywrwater.module.ht.dal.dataobject.waterqualityalertcount.WaterQualityAlertCountDO;
import cn.ht.jywrwater.framework.common.pojo.PageResult;

import cn.ht.jywrwater.module.ht.convert.waterqualityalertcount.WaterQualityAlertCountConvert;
import cn.ht.jywrwater.module.ht.dal.mysql.waterqualityalertcount.WaterQualityAlertCountMapper;

import static cn.ht.jywrwater.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.ht.jywrwater.module.ht.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 水质统计 Service 实现类
 *
 * @author hzx
 */
@Service
@Validated
public class WaterQualityAlertCountServiceImpl implements WaterQualityAlertCountService {

    @Resource
    private WaterQualityAlertCountMapper waterQualityAlertCountMapper;

    @Override
    public Long createWaterQualityAlertCount(WaterQualityAlertCountCreateReqVO createReqVO) {
        // 插入
        WaterQualityAlertCountDO waterQualityAlertCount = WaterQualityAlertCountConvert.INSTANCE.convert(createReqVO);
        waterQualityAlertCountMapper.insert(waterQualityAlertCount);
        // 返回
        return waterQualityAlertCount.getId();
    }

    @Override
    public void updateWaterQualityAlertCount(WaterQualityAlertCountUpdateReqVO updateReqVO) {
        // 校验存在
        validateWaterQualityAlertCountExists(updateReqVO.getId());
        // 更新
        WaterQualityAlertCountDO updateObj = WaterQualityAlertCountConvert.INSTANCE.convert(updateReqVO);
        waterQualityAlertCountMapper.updateById(updateObj);
    }

    @Override
    public void deleteWaterQualityAlertCount(Long id) {
        // 校验存在
        validateWaterQualityAlertCountExists(id);
        // 删除
        waterQualityAlertCountMapper.deleteById(id);
    }

    private void validateWaterQualityAlertCountExists(Long id) {
        if (waterQualityAlertCountMapper.selectById(id) == null) {
            throw exception(WATER_QUALITY_ALERT_COUNT_NOT_EXISTS);
        }
    }

    @Override
    public WaterQualityAlertCountDO getWaterQualityAlertCount(Long id) {
        return waterQualityAlertCountMapper.selectById(id);
    }

    @Override
    public List<WaterQualityAlertCountDO> getWaterQualityAlertCountList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return waterQualityAlertCountMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<WaterQualityAlertCountDO> getWaterQualityAlertCountPage(WaterQualityAlertCountPageReqVO pageReqVO) {
        return waterQualityAlertCountMapper.selectPage(pageReqVO);
    }

    @Override
    public List<WaterQualityAlertCountDO> getWaterQualityAlertCountList(WaterQualityAlertCountExportReqVO exportReqVO) {
        return waterQualityAlertCountMapper.selectList(exportReqVO);
    }

}
